package com.train.controller;

import com.train.entity.User;
import com.train.service.UserService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping(path = {"/user"})
public class UserController {
    private final UserService userService;
    public UserController(UserService userService) {
        this.userService = userService;
    }

    @RequestMapping(method = {RequestMethod.GET})
    public ResponseEntity<List<User>> getUser() {
        List<User> users = userService.getUsers();
        return new ResponseEntity<>(users, HttpStatus.OK);
    }
    /**
     * 保存用户
     * */
    @RequestMapping(method = RequestMethod.POST)
    public ResponseEntity<Object> createUser(@RequestBody List<User> users) {
        if (users.isEmpty()) {
            return new ResponseEntity<>("no data", HttpStatus.BAD_REQUEST);
        }else {
            userService.saveUsers(users);
            users.forEach(System.out::println);
            return new ResponseEntity<>("saved", HttpStatus.OK);
        }
    }
}
